package com.base.core.context.utils.teams;


import java.util.ArrayList;
import java.util.List;

import com.gitee.magic.context.ConverterEditorUtils;
import com.gitee.magic.framework.base.rest.HttpRequest;
import com.gitee.magic.framework.base.rest.HttpSsl;
import com.gitee.magic.framework.base.rest.HttpWrapper;
import com.gitee.magic.framework.base.rest.RequestMethodEnum;

/**
 * @see https://docs.microsoft.com/zh-cn/outlook/actionable-messages/send-via-connectors
 * @author start
 *
 */
public class TeamsMessage {

    public void send(String url,String title,String message){
        List<TeamsBO.Body> bodyList = new ArrayList<>();
        TeamsBO.Body teamsTitle = new TeamsBO.Body();
        teamsTitle.setType("TextBlock");
        teamsTitle.setText(title);
        teamsTitle.setWeight("bolder");
        teamsTitle.setSize("medium");
        bodyList.add(teamsTitle);
        
        TeamsBO.Body teamsBody= new TeamsBO.Body();
        teamsBody.setType("TextBlock");
        teamsBody.setText(message);
        teamsBody.setWrap(true);
        bodyList.add(teamsBody);
        
        TeamsBO markdown = createTeams(bodyList);
        requestHttp(url,markdown);
    }

    public TeamsBO createTeams(List<TeamsBO.Body> bodyList) {
        TeamsBO markdown = new TeamsBO();
        markdown.setType("message");
        List<TeamsBO.Attachments> attachments = new ArrayList<TeamsBO.Attachments>();
        TeamsBO.Attachments attach = new TeamsBO.Attachments();
        attach.setContentType("application/vnd.microsoft.card.adaptive");
        TeamsBO.Content content = new TeamsBO.Content();
        content.setVersion("1.2");
        content.setType("AdaptiveCard");
        content.setBody(bodyList);
        attach.setContent(content);
        attachments.add(attach);
        markdown.setAttachments(attachments);
        return markdown;
    }
    
    public void requestHttp(String url,TeamsBO bo) {
    	 HttpSsl.setDefaultHttps();
         HttpRequest request = new HttpRequest(url);
         request.setRequestMethod(RequestMethodEnum.POST);
         request.setHeader(HttpWrapper.CONTENTTYPE, HttpWrapper.CONTENTTYPE_JSON);
         request.setBodyContent(ConverterEditorUtils.converterObject(bo).toString());
         HttpWrapper wrapper=new HttpWrapper();
         String content=wrapper.start(request);
         System.out.println(content);
    }

    public static void main(String[] args) {
        String url = "https://prod-77.westus.logic.azure.com:443/workflows/ed265c59cf8143e7a0af6b3a2873615b/triggers/manual/paths/invoke?api-version=2016-06-01&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig=49tj9s4VsHldFz8mSQzizh20Ez9drEPe9Xbg9PTnsQU";
        TeamsMessage message = new TeamsMessage();
        message.send(url,"测试标题","每日");
    }
    
}
